// pages/userInfo/userInfo.js

var wxNetwork = require("../../utils/network.js");
var wxURL = require("../../utils/requestURL.js");

Page({

  data: {
    userNum: '',
    userName: '',
    userPhone: '',
    userAddress: '',
    userType: '',

    waterUserInfo:{},
    isNumInput: true,
    isTelInput: true
  },

  onLoad: function (options) {
    
    var that = this;
    that.setData({
      waterUserInfo: JSON.parse(options.detail)
    })

    that.setData({
      userNum: that.data.waterUserInfo.waterUserNum,
      userName: that.data.waterUserInfo.waterUserName,
      userPhone: that.data.waterUserInfo.waterUserPhone,
      userAddress: that.data.waterUserInfo.waterUserAddress,
      userType: that.data.waterUserInfo.waterUserType
    })
    
    if (that.data.waterUserInfo.authMethod === '0') { //户号认证方式：户号不可改，手机号可改
      that.setData({
        isNumInput: true,
        isTelInput: false
      })
    }
    else {  //手机认证方式：户号和手机号不可改
      that.setData({
        isNumInput: true,
        isTelInput: true
      })
    }
  },

  /** 姓名输入 */
  nameInput: function (e) {
    var that = this;
    that.data.userName = e.detail.value;
  },

  /** 电话输入 */
  telInput: function (e) {
    var that = this;
    that.data.userPhone = e.detail.value;
  },

  /** 地址输入 */
  addressInput: function (e) {
    var that = this;
    that.data.userAddress = e.detail.value;
  },

  /** 家庭类型 */
  iFamilyInput: function (e) {
    var that = this;
    that.data.userType = e.detail.value;
  },

  /** 修改按钮 */
  cilckButton:function(e){

    var that = this;

    var header = {
      'Access-Token': wx.getStorageSync('token'),
      'content-type': 'application/x-www-form-urlencoded'
    };
    var params = {
      'wxappuserId':parseInt(that.data.waterUserInfo.wxappuserId),
      'waterUserName': (that.data.userName == null ? '' : that.data.userName),
      'waterUserAddress':(that.data.userAddress == null ? '': that.data.userAddress),
      'waterUserType': (that.data.userType == null ? '' : that.data.userType),
      'waterUserPhone': (that.data.userPhone == null ? '' : that.data.userPhone)
    };

    console.log(params);

    wxNetwork.putRequest(wxURL.put_updateUsers_URL, params, header, function (dict) {
      feedbackApi.showToast({
        title: dict.message.notice,
        icon: '/images/others/tishi_dui.png'
      })
       
      wx.switchTab({
        url: '../home/home',
      })
    },
      function (dict) {

      },
      function (dict) {

      })
  }
})